import Data.Char
import Control.Monad

intDigits :: Integer -> [Int]
intDigits n = map (\x -> read [x] :: Int) (show n)

charDigits :: [Int] -> [Char]
charDigits = map (\x -> intToDigit x)

digits :: Integer -> [Char]
digits = charDigits . intDigits

divide :: Integer -> Char -> Bool
divide _ '0' = False
divide n c = isMultiple
    where digit = toInteger $ digitToInt c
          nModDigit = n `mod` digit
          isMultiple = nModDigit == 0
              
findDigit :: Integer -> Int
findDigit n = foldl (\a b -> a + (if div b then 1 else 0)) 0 list
    where div = divide n
          list = digits n         

getStrings :: Integer -> [IO String]
getStrings n 
    | n <= 0 = []
    | otherwise = getLine : getStrings (n - 1)
    
getIntegers :: [String] -> [Integer]
getIntegers [] = []
getIntegers (x:xs) = readInteger x : getIntegers xs

readInteger :: String -> Integer
readInteger = read

main = do    
    qtd <- getLine
    valStr <- sequence $ getStrings (readInteger qtd)    
    let valores = getIntegers valStr
        digitsFound = map (findDigit) valores    
    mapM (print) digitsFound                 
